var table;
$(function(){
    table=$("#blacklist").DataTable({
        "ajax": {
            url:"../../admin/blacklist/Get",
            type:"POST",
            data:function(d){
                d.name=$("#blacklist_name").val()
                d.mobile=$("#blacklist_mobile").val()
            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "scrollX": 1200,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": true,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [2,5,10],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            { "data": "id" },
            { "data": "name" },
            { "data": "mobile" },
        ],
        "aoColumnDefs": [
            {
                "aTargets": [ 1 ],
                "mRender": function ( data, type, full ) {
                    return '<input type="checkbox" name="tab_box" value="'+data+'">';
                }
            },
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
})

function search_blacklist(){
    table.draw();
}

function blacklist_box() {
    if($("input[value='blacklist_box']").prop("checked")){
        $("input[name='tab_box']").prop("checked",true);
    }
    else {
        $("input[name='tab_box']").prop("checked",false);
    }
}

/* 批量删除 1=删除*/
function del_blacklist() {
// 判断是否至少选择一项
    var checkedNum = $("input[name='tab_box']:checked").length;
    if(checkedNum == 0) {
        alert("请选择至少一项！");
        return;
    }
// 批量选择
    var checkedList = [];
    $("input[name='tab_box']").each(function() {
        if($(this).is(':checked')){
            checkedList.push($(this).val());
        }
    });
    var SendData = {'Ids':checkedList.toString()
        ,'process':1 }
    SendData = JsonToSecurityString(SendData)
    Lobibox.confirm({
        msg: "确认要删除这些条目?",
        title:"删除条目",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type==='yes'){
                $.ajax({
                    type: "POST",
                    url: "../../admin/blacklist/smsBlackMore",
                    data: SendData,
                    success: function(data) {
                        if(data){
                            table.draw();
                            Lobibox.notify('success', {
                                msg: '操作成功',
                                sound: false
                            });
                        }else {
                            Lobibox.notify('error', {
                                msg: '操作失败',
                                sound: false
                            });
                        }
                        $("input[name='tab_box']").prop("checked", false);
                        // window.location.reload();
                    }
                });
            }
        }
    });
}

/**
 * @return {string}
 */
function JsonToSecurityString(SendData) {
    var eachData = {};
    eachData["_csrf"]=$("input[name=_csrf]").val();
    for(var i in SendData){
        eachData[i]=SendData[i];
    }
    var sendStr = "";
    for(var key in eachData){
        if (key=="_csrf"){
            sendStr += key + '=' + eachData[key];
        }else{
            sendStr += '&' + key + '=' + eachData[key];
        }
    }
    return sendStr;
}